<template>
  <div class="carousel" @mouseenter="stop" @mouseleave="start">
    <!-- 轮播图 -->
    <ul class="carousel-body">
        <li class="carousel-item" v-for="(item,i) in silders" :key="i" :class="{fade:index===i}">
            <router-link to="/">
                <img :src="item.imgUrl" alt="">
            </router-link>
        </li>
    </ul>
    <!-- 左右箭头 -->
    <a href="javascript:;" class="carousel-btn preBtn" @click="lastFn"><i class="iconfont icon-angle-left"></i></a>
    <a href="javascript:;" class="carousel-btn nextBtn" @click="nextFn"><i class="iconfont icon-angle-right"></i></a>
    <!-- 小圆点 指示器-->
    <div class="carousel-indicator">
        <span v-for="(item,i) in silders" :key="i" :class="{active:index===i}" @click="toggleFn(i)"></span>
    </div>
  </div>
</template>

<script>
import { ref, watch } from 'vue'
export default {
  name: 'syCarousel',
  props: {
    // 图片数组信息
    silders: {
      type: Array,
      default: () => []
    },
    // 自动轮播时间
    duration: {
      type: Number,
      default: 3000
    },
    // 是否开启自动轮播
    autoPlay: {
      type: Boolean,
      default: false
    }
  },
  setup (props) {
    // 显示图片的索引
    const index = ref(0)

    // 自动轮播
    let timer = null
    const autoPlayFn = () => {
      timer && clearInterval(timer)
      timer = setInterval(() => {
        index.value++
        if (index.value >= props.silders.length) {
          index.value = 0
        }
      }, props.duration)
    }

    // 监听props.silders是否有数据，并且是否开启自动轮播
    watch(() => props.silders, (newVal) => {
      // 有数据才能开启自动轮播
      if (newVal.length && props.autoPlay) {
        index.value = 0
        autoPlayFn()
      }
    }, { immediate: true })

    // 鼠标进入暂停轮播
    const stop = () => {
      timer && clearInterval(timer)
    }

    // 鼠标离开，开启轮播
    const start = () => {
      if (props.silders.length && props.autoPlay) autoPlayFn()
    }

    // 点击上一张
    const lastFn = () => {
      index.value--
      if (index.value < 0) index.value = props.silders.length - 1
    }
    // 点击下一张
    const nextFn = () => {
      index.value++
      if (index.value >= props.silders.length) index.value = 0
    }

    // 点击指示器
    const toggleFn = (i) => {
      index.value = i
    }
    return { index, stop, start, lastFn, nextFn, toggleFn }
  }
}
</script>

<style lang="less" scoped>
.carousel{
    width: 100%;
    height: 100%;
    min-width: 300px;
    min-height: 500px;
    position: relative;

    .carousel-body{
        width: 100%;
        height: 100%;
        .carousel-item{
            width: 100%;
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            transition: opacity 0.5s linear;
            &.fade{
                opacity: 1;
                z-index: 1;
            }
            img{
                width: 100%;
                height: 100%;
            }
        }
    }
    &:hover .carousel-btn{
        opacity: 1;
    }
    .carousel-btn{
        width: 44px;
        height: 44px;
        background: rgba(0,0,0,.2);
        color:#fff;
        border-radius: 50%;
        position: absolute;
        top:228px ;
        z-index: 2;
        text-align: center;
        line-height: 44px;
        opacity: 0;
        transition: all 0.5s;
        &.preBtn{
            left: 20px;
        }
        &.nextBtn{
            right: 20px;
        }
    }
    .carousel-indicator{
        position: absolute;
        left: 0;
        bottom: 20px;
        z-index: 2;
        width: 100%;
        text-align: center;
        span{
            display: inline-block;
            width: 12px;
            height: 12px;
            background: rgba(0, 0, 0, 0.2);
            border-radius: 50%;
            cursor: pointer;
            ~ span{
                margin-left: 12px;
            }
            &.active{
                background: #fff;
            }
        }
    }
}
</style>
